package game;

import java.util.ArrayList;
import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

public class TitleState  extends BasicGameState 
{
  private ArrayList <String>menus;
  private Image logo,titlebg,screen;
  public static final int ID = 0;	
  private Font font;
  private boolean menuNext,menuPrev;
  public static Music titleMusic;
  private Rectangle menuRect[];
  private int menuOffset, oldOffset;
  private StateBasedGame game;
  public int getID() 
  {
    return ID;
  }

  public void init(GameContainer container, StateBasedGame game) throws SlickException 
  {
    this.game = game;  
    titlebg = new Image("data/image/intro2.png");
    logo = new Image("data/image/sweaselLogo.png");
    font = new AngelCodeFont("data/font/weasel.fnt","data/font/weasel.png");
    screen = new Image("data/image/ctrlScreen4.png");
    menus = new ArrayList<String>();	
    menus.add("Start a new game");
    menus.add("Options & Controls");
    menus.add("Load previous game");
    menus.add("Save current game");
    menuRect = new Rectangle[menus.size()];
  }

  public void update(GameContainer container, StateBasedGame game, int delta)
  {
    if (menuOffset>=0)  {   menuPrev=false;   }
    if (menuOffset<=-900)  {  menuNext=false;  }
    if (menuNext)
    {
      menuOffset -= 10;
      if(menuOffset <= oldOffset - 300){
        menuNext=false;
      }
    }
    if (menuPrev)
    {
      menuOffset += 10;
      if(menuOffset >= oldOffset + 300){
        menuPrev=false;
      }
    }
  }

  public void render(GameContainer container, StateBasedGame game, Graphics g) 
  {
    g.scale(2, 2);
    g.drawImage(titlebg,0,0);
    g.drawImage(logo, (400-logo.getWidth())/2, 20);
    g.drawImage(screen, 0, 192);
    g.setColor(Color.white);
    g.setFont(font);
    for (int i=0;i<menus.size();i++)
    {
      g.drawString(menus.get(i),(130+(menuOffset)+i*300), 205);
      menuRect[i]=new Rectangle((130+(menuOffset)+i*300), 205, 150, 25);
    }
    g.resetTransform();
  }

  public void  leftFlick()
  {
    menuNext=true;
    oldOffset = menuOffset;
  }
  public void  rightFlick()
  {
    menuPrev=true;
    oldOffset = menuOffset;
  }

  public void  mouseDragged(int oldx, int oldy, int newx, int newy) 
  {
    if (oldx>newx && !menuNext && !menuPrev)
    {
      if (oldx-newx>50)
      {
        leftFlick();
      }
    }
    if (newx>oldx && !menuNext && !menuPrev)
    {
      if (newx-oldx>50)
      {
        rightFlick();
      }
    }
  }
  public void mousePressed(int button, int x, int y)
  { 
    switch (menuOffset)
    {
    case 0:
      if (menuRect[0].contains(x/2, y/2))
      {
        game.enterState(GameState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
        System.out.printf("rectangle[0] pressed\n");
      break;
      }
    case -300:
      if (menuRect[1].contains(x/2, y/2))
        System.out.printf("rectangle[1] pressed\n");
      break; 
    case -600:
      if (menuRect[2].contains(x/2, y/2))
        System.out.printf("rectangle[2] pressed\n");
      break; 
    case -900:
      if (menuRect[3].contains(x/2, y/2))
        System.out.printf("rectangle[3] pressed\n");
      break;  
    }
  }
}
